package com.google.code.syncgear.plugin;

import java.io.Serializable;
import java.util.List;

import com.google.code.syncgear.plugin.bean.FetchResult;
import com.google.code.syncgear.util.CacheUtil;

public abstract class AbstractDispatch implements Serializable {

	private static final long serialVersionUID = 3781638115510365001L;
	
	protected abstract boolean init(List<String> target);	
	protected abstract boolean auth(List<String> target);
	protected abstract boolean process(List<String> target, FetchResult fetchResult);
	
	public final boolean fire(List<String> target, FetchResult fetchResult) {
		if(init(target))
			if(auth(target))
				if(process(target, fetchResult))
					return true;
		return false;
	}
	
	protected final void freeze(List<String> target, Object object) {
		CacheUtil.put(freezeObjectNameGetter(target), object);
	}
	
	protected final Object unfreeze(List<String> target) {
		Object result = CacheUtil.get(freezeObjectNameGetter(target));
		return result;
	}
	
	private String freezeObjectNameGetter(List<String> target) {
		StringBuffer stringBuffer = new StringBuffer(this.getClass().getName());
		for(String item : target) {
			stringBuffer.append(item);
		}
		return stringBuffer.toString();
	}

}